package com.gdpu.pbss.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdpu.pbss.Constants.Constants;
import com.gdpu.pbss.common.HoneyLogs;
import com.gdpu.pbss.common.LogType;
import com.gdpu.pbss.common.Result;
import com.gdpu.pbss.pojo.Files;
import com.gdpu.pbss.pojo.Pb;
import com.gdpu.pbss.pojo.Pbss;
import com.gdpu.pbss.service.PbService;
import com.gdpu.pbss.service.PbssService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Pbss")
@Slf4j
public class PbssController {

    @Autowired
    private PbssService pbssService;

    @Autowired
    private PbService pbService;


    @GetMapping("/page")
    public Result selectPbssPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "") String pbssName,
                                 @RequestParam(defaultValue = "")  Integer pbssId
                               )
    {


        IPage<Pbss> page=new Page<>(pageNum,pageSize);
        QueryWrapper<Pbss> qw =new QueryWrapper<>();

        qw.eq(pbssId!=null,"pbss_id",pbssId);
        qw.like(pbssName!=null,"pbss_name",pbssName);
//        if (!"".equals(pbssName)) {
//            qw.like("pbss_name", pbssName);
//        }
        qw.orderByDesc("pbss_id");


        return Result.success(pbssService.page(page,qw));

    }
    @HoneyLogs(operation = "投放点管理",type = LogType.UPDATE)
    @PostMapping("/update")
    public Result updatePbss(@RequestBody Pbss pbss){
        QueryWrapper<Pb> qw=new QueryWrapper<>();
        qw.eq("pbss_name",pbss.getPbssName());
        List<Pb> pbs = pbService.list(qw);
        for (Pb pb : pbs) {
            pb.setEnable(pbss.getEnable());
            pbService.updateById(pb);
        }

        return Result.success(pbssService.updateById(pbss));
    }
    @HoneyLogs(operation = "投放点管理",type = LogType.ADD)
    @PostMapping("/save")
    public Result savePbss(@RequestBody Pbss pbss){
        if (pbss.getPbssId()==null){
         return  Result.success(pbssService.save(pbss))   ;
        }else {
            QueryWrapper<Pb> qw=new QueryWrapper<>();
            qw.eq("pbss_id",pbss.getPbssId());
            List<Pb> pbs = pbService.list(qw);
            for (Pb pb : pbs) {
                pb.setPbssName(pbss.getPbssName());
                pbService.updateById(pb);
            }



            return Result.success(pbssService.updateById(pbss)) ;
        }
    }

    @HoneyLogs(operation = "投放点管理",type = LogType.DELETE)
    @DeleteMapping("/{pbssId}")
    public Result delPbss(@PathVariable("pbssId") Integer pbssId){
        Pbss pbss = pbssService.getById(pbssId);
        if (pbss.getPbCount()>0){
            return Result.error(Constants.CODE_401,"删除失败,请清空所有充电宝再删除");
        }else {
            pbssService.removeById(pbss);
            return Result.success();
        }
    }

    @HoneyLogs(operation = "投放点管理",type = LogType.BATCH_DELETE)
    @PostMapping("/del/batch")
    public Result delpatchPbss(@RequestBody List<Integer> ids){
        QueryWrapper<Pbss> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("pbss_id",ids);
        List<Pbss> pbssList = pbssService.list(queryWrapper);
        for (Pbss pbss : pbssList) {
            if (pbss.getPbCount()>0){
                return  Result.error(Constants.CODE_401,"批量删除失败,请清空所有充电宝再删除");
            }else
                pbssService.removeById(pbss);
            }
        return Result.success();
        }

        @GetMapping("/pbssName")
    public Result getPbssName(){

        List<String> pbssName=new ArrayList<String>();
        QueryWrapper<Pbss> qw=new QueryWrapper<>();
        qw.in("enable",true);
            List<Pbss> pbssList = pbssService.list(qw);
            for (Pbss pbss : pbssList) {
                String name = pbss.getPbssName();
                pbssName.add(name);
            }
            return Result.success(pbssName);
        }

        //获取经纬度

    @GetMapping("/list")

    public Result list(){


        List<Pbss> pbsses = pbssService.list();

//        Map<String, BigDecimal> adrmap=new HashMap<>();
//        for (Pbss pbss : pbsses) {
//            if (pbss.getAddrX()!=null&&pbss.getAddrY()!=null){
//                adrmap.put("lng",pbss.getAddrY());
//                adrmap.put("lat",pbss.getAddrX());
//                System.out.println(adrmap);
//            }
//
//        }



        return Result.success(pbsses);


    }

    }


